<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <el-row style="height: 60px;">
              <el-button
                icon="el-icon-plus"
                type="primary"
                size="small"
                @click="showDialog=true"
              >新增角色</el-button>
            </el-row>
            <el-table border="" :data="list">
              <el-table-column align="center" type="index" label="序号" width="120" />
              <el-table-column align="center" prop="name" label="名称" width="240" />
              <el-table-column align="center" prop="description" label="描述" />
              <el-table-column align="center" label="操作">
                <template slot-scope="{row}">
                  <el-button size="small" type="success" @click="assinPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editBtn(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRoleDetail(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" justify="center" align="middle" style="height:60;">
              <el-pagination
                :total="page.total"
                :current-page="page.page"
                :page-size="page.pagesize"
                layout="prev,pager,next"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">
            <el-alert type="info" :show-icon="true" :closable="false" title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改" />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="企业名称">
                <el-input v-model="daDate.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="daDate.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="电话">
                <el-input v-model="daDate.companyPhone" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="daDate.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="daDate.remarks" type="textarea" :row="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-dialog :visible="showDialog" title="编辑部门" @click="btnCancel">
            <el-form ref="formData" :model="formData" :rules="rules" label-width="120px">
              <el-form-item label="角色名称" prop="name">
                <el-input v-model="formData.name" />
              </el-form-item>
              <el-form-item label="角色描述">
                <el-input v-model="formData.description" />
              </el-form-item>
            </el-form>
            <el-row slot="footer" type="flex" justify="center">
              <el-col :span="6">
                <el-button size="small" @click="btnCancel">取消</el-button>
                <el-button size="small" type="primary" @click="btnOk">确定</el-button>
              </el-col>
            </el-row>
          </el-dialog>
        </el-tabs>
      </el-card>
      <el-dialog title="分配权限" :visible="showPerm">
        <el-tree
          ref="premTree"
          :data="permData"
          :props="defaultProps"
          :show-checkbox="true"
          :default-expand-all="true"
          :check-strictly="true"
          :default-checked-keys="selectCheck"
          node-key="id"
        />
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="btnPermOk">确定</el-button>
            <el-button size="small" type="damage" @click="btnPermCencel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoleList, getCompanyId, delRoleDetail, getRoleDetail, updateRole, addRole, assignPerm } from '@/api/setting'
import { mapGetters } from 'vuex'
import { tranListToTreeData } from '@/utils'
import { getPermissionList } from '@/api/permission'
export default {
  data() {
    return {
      activeName: 'first',
      page: {
        page: 1,
        total: 0,
        pagesize: 10
      },
      list: [],
      daDate: {},
      formData: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' }]
      },
      showDialog: false,
      showPerm: false,
      permData: [],
      defaultProps: {
        label: 'name'
      },
      userId: null,
      selectCheck: [],
      roleId: null
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getRoleList()
    this.getCompanyId()
  },
  methods: {
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },
    changePage(newPage) {
      this.page.page = newPage
      this.getRoleList()
    },
    async getCompanyId() {
      this.daDate = await getCompanyId(this.companyId)
      console.log(2)
    },
    async  getRoleDetail() {
      this.formData = await getRoleDetail()
    },
    async delRoleDetail(id) {
      try {
        await delRoleDetail(id)
        this.getRoleList() // 重新加载数据
        this.$message.success('删除成功')
      } catch (err) {
        console.log(err)
      }
    },
    async editBtn(id) {
      this.formData = await getRoleDetail(id)
      this.showDialog = true
    },
    async btnOk() {
      try {
        await this.$refs.formData.validate()
        if (this.formData.id) {
          updateRole(this.formData)
        } else {
          await addRole(this.formData)
        }
        this.getRoleList() // 重新加载数据
        this.showDialog = false
        this.$message.success('编辑成功')
      } catch (err) {
        console.log(err)
      }
    },
    btnCancel() {
      this.formData = {
        name: '',
        description: ''
      }
      this.$refs.formData.resetFields()
      this.showDialog = false
    },
    async assinPerm(id) {
      this.permData = tranListToTreeData(await getPermissionList(), '0')
      this.roleId = id
      const { permIds } = await getRoleDetail(id)
      this.selectCheck = permIds
      this.showPerm = true
    },
    async btnPermOk() {
      await assignPerm({ permIds: this.$refs.premTree.getCheckedKeys(), id: this.roleId })
      this.$message.success('分配成功')
      this.showPerm = false
    },
    btnPermCencel() {
      this.selectCheck = []
      this.showPerm = false
    }
  }
}
</script>

<style>

</style>
